package managedBean;

import Merlion.MRP.bean.DemandManagementBeanRemote;
import Merlion.SCM.bean.InventoryItemsBeanRemote;
import Merlion.util.state.InventoryState;
import Merlion.util.state.PlannedDemandState;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;

@ManagedBean(name = "MPSDailyManagedBean")
@ViewScoped
public class MPSDailyManagedBean {

    private List<PlannedDemandState> MPSList1;
    private List<PlannedDemandState> MPSList2;
    private List<PlannedDemandState> MPSList3;
    private List<PlannedDemandState> MPSList4;
    private Integer selectedYear;
    private Integer selectedMonth;
    private Integer selectedWeek;
    private List<InventoryState> productList;
    private String name1;
    private String name2;
    private String name3;
    private String name4;
    private Long id1;
    private Long id2;
    private Long id3;
    private Long id4;
    
    @EJB
    InventoryItemsBeanRemote InventoryItemsBean;
    @EJB
    DemandManagementBeanRemote DemandManagementBean;
    
    public MPSDailyManagedBean() {
    }
    
    public void initView(PhaseEvent event) {
        System.out.println("**MPSDailyDemandManagedBean: initView()");
        
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (productList == null) {
            productList = InventoryItemsBean.viewProducts();
            productList.size();
            name1 = productList.get(0).getName();
            name2 = productList.get(1).getName();
            name3 = productList.get(2).getName();
            name4 = productList.get(3).getName();
            id1 = productList.get(0).getItemId();
            id2 = productList.get(1).getItemId();
            id3 = productList.get(2).getItemId();
            id4 = productList.get(3).getItemId();
        }
        
        if (MPSList1 == null) {
            List<PlannedDemandState> tempAllList = DemandManagementBean.viewPlannedDemands(2);
            tempAllList.size();
            List<PlannedDemandState> tempMPSList1 = new ArrayList();
            List<PlannedDemandState> tempMPSList2 = new ArrayList();
            List<PlannedDemandState> tempMPSList3 = new ArrayList();
            List<PlannedDemandState> tempMPSList4 = new ArrayList();

            for (Object o : tempAllList) {
                PlannedDemandState pd = (PlannedDemandState) o;
                if (pd.getProductId() == id1 && pd.getWeekOfMonth().intValue() == 3 && pd.getMonthInt().intValue() == 11 && pd.getYearInt().intValue() == 2011) {
                    tempMPSList1.add(pd);
                } else if (pd.getProductId() == id2 && pd.getWeekOfMonth().intValue() == 3 && pd.getMonthInt().intValue() == 11 && pd.getYearInt().intValue() == 2011) {
                    tempMPSList2.add(pd);
                } else if (pd.getProductId() == id3 && pd.getWeekOfMonth().intValue() == 3 && pd.getMonthInt().intValue() == 11 && pd.getYearInt().intValue() == 2011) {
                    tempMPSList3.add(pd);
                } else if (pd.getProductId() == id4 && pd.getWeekOfMonth().intValue() == 3 && pd.getMonthInt().intValue() == 11 && pd.getYearInt().intValue() == 2011) {
                    tempMPSList4.add(pd);
                } else {
                    ;
                }
            }
            MPSList1 = tempMPSList1;
            MPSList2 = tempMPSList2;
            MPSList3 = tempMPSList3;
            MPSList4 = tempMPSList4;
        }
    }
    
    public void doGenerateDailyMPS(ActionEvent event) {
        System.out.println("**MPSDailyManagedBean: doGenerateDailyMPS()");
        
        List<PlannedDemandState> tempMPSList1 = DemandManagementBean.generateDailyPlannedDemands(productList.get(0).getItemId(), selectedYear, selectedMonth, selectedWeek);
        List<PlannedDemandState> tempMPSList2 = DemandManagementBean.generateDailyPlannedDemands(productList.get(0).getItemId(), selectedYear, selectedMonth, selectedWeek);
        List<PlannedDemandState> tempMPSList3 = DemandManagementBean.generateDailyPlannedDemands(productList.get(0).getItemId(), selectedYear, selectedMonth, selectedWeek);
        List<PlannedDemandState> tempMPSList4 = DemandManagementBean.generateDailyPlannedDemands(productList.get(0).getItemId(), selectedYear, selectedMonth, selectedWeek);
    
        this.setMPSList1(tempMPSList1);
        this.setMPSList2(tempMPSList2);
        this.setMPSList3(tempMPSList3);
        this.setMPSList4(tempMPSList4);
    }
    
    public void doCommitMPS(ActionEvent event) throws IOException {
        System.out.println("**MPSDailyManagedBean: doCommitMPS()");
        
        boolean commitStatus = DemandManagementBean.commitMonthDailyPlannedDemand(selectedYear, selectedMonth);

        if (commitStatus == true) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Notification", "You have successfully commit the selected MPS!"));
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Notification", "You failed to commit the selected MPS!"));;
        }
    }

    public List<PlannedDemandState> getMPSList1() {
        return MPSList1;
    }

    public void setMPSList1(List<PlannedDemandState> MPSList1) {
        this.MPSList1 = MPSList1;
    }

    public List<PlannedDemandState> getMPSList2() {
        return MPSList2;
    }

    public void setMPSList2(List<PlannedDemandState> MPSList2) {
        this.MPSList2 = MPSList2;
    }

    public List<PlannedDemandState> getMPSList3() {
        return MPSList3;
    }

    public void setMPSList3(List<PlannedDemandState> MPSList3) {
        this.MPSList3 = MPSList3;
    }

    public List<PlannedDemandState> getMPSList4() {
        return MPSList4;
    }

    public void setMPSList4(List<PlannedDemandState> MPSList4) {
        this.MPSList4 = MPSList4;
    }

    public Integer getSelectedMonth() {
        return selectedMonth;
    }

    public void setSelectedMonth(Integer selectedMonth) {
        this.selectedMonth = selectedMonth;
    }

    public Integer getSelectedYear() {
        return selectedYear;
    }

    public void setSelectedYear(Integer selectedYear) {
        this.selectedYear = selectedYear;
    }

    public InventoryItemsBeanRemote getInventoryItemsBean() {
        return InventoryItemsBean;
    }

    public void setInventoryItemsBean(InventoryItemsBeanRemote InventoryItemsBean) {
        this.InventoryItemsBean = InventoryItemsBean;
    }

    public String getName1() {
        return name1;
    }

    public void setName1(String name1) {
        this.name1 = name1;
    }

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    public String getName3() {
        return name3;
    }

    public void setName3(String name3) {
        this.name3 = name3;
    }

    public String getName4() {
        return name4;
    }

    public void setName4(String name4) {
        this.name4 = name4;
    }

    public List<InventoryState> getProductList() {
        return productList;
    }

    public void setProductList(List<InventoryState> productList) {
        this.productList = productList;
    }

    public Integer getSelectedWeek() {
        return selectedWeek;
    }

    public void setSelectedWeek(Integer selectedWeek) {
        this.selectedWeek = selectedWeek;
    }
    
}
